package com.example.hrmsaas.system.controller;



import com.example.hrmsaas.system.entity.Role;
import com.example.hrmsaas.system.service.IRoleService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.ResultObject;
import com.example.hrmsaas.system.utils.vo.RoleVo;
import com.example.hrmsaas.system.utils.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/system/sys-role")
public class RoleController extends BaseController {

    private Log log = (Log) LogFactory.getLog(RoleController.class);

    @Autowired
    IRoleService roleService;

    @RequestMapping("/roleList")

    public DataGridView getUserList() throws ParseException {
        try {
            RoleVo roleVo = new RoleVo();
            Integer page = request.getParameter("page") != null ? Integer.valueOf(request.getParameter("page")) : null;
            Integer limit = request.getParameter("limit") != null ? Integer.valueOf(request.getParameter("limit")) : null;
            RoleVo roleVoQueryMapper = this.getRoleVoQueryMapper(roleVo, page, limit, CompanyId);
            return roleService.getRoleList(roleVoQueryMapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new DataGridView();
        }

    }

    private RoleVo getRoleVoQueryMapper(RoleVo roleVo, Integer page, Integer limit, String companyId) {
        if (null != page) roleVo.setPage(page);
        if (null != limit) roleVo.setLimit(limit);
        roleVo.setCompanyId(companyId);
        return roleVo;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultObject addUser(@RequestBody Role role) throws ParseException {
        try {
            role.setCompanyId(CompanyId);
            if (roleService.addRold(role)) {
                return ResultObject.ADD_SUCCESS;
            } else {
                return ResultObject.ADD_ERROR;
            }
        } catch (Exception e) {
            log.error("新增角色异常" + e.getMessage());
            return ResultObject.ADD_ERROR;
        }

    }

    @RequestMapping("/info")
    public DataGridView getRoleInfo() {
        if (!StringUtils.isEmpty(request.getParameter("id"))) {
           return roleService.getRoleInfoById(request.getParameter("id"));
        } else {
            return new DataGridView();
        }
    }

    @RequestMapping(value = "setPrem",method = RequestMethod.POST)
    public ResultObject setPrem(@RequestBody Map<String,Object> map) {
        String userId = (String) map.get("roleId");
        List<String> prmeIds = (List<String>) map.get("ids");
        if(roleService.setPrem(userId,prmeIds)){
            return ResultObject.DISPATCH_SUCCESS;
        }else{
            return ResultObject.DISPATCH_ERROR;
        }

    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultObject editUser(@RequestBody Role role) {
        try {
            if (roleService.editRold(role)) {
                return ResultObject.SAVE_SUCCESS;
            } else {
                return ResultObject.SAVE_ERROR;
            }
        } catch (Exception e) {
            log.error("编辑角色出现异常：" + e.getMessage());
            return ResultObject.SAVE_ERROR;
        }
    }

    @RequestMapping("/del")
    public ResultObject delRole() throws ParseException {
        if (!StringUtils.isEmpty(request.getParameter("id"))) {
            if (roleService.delRold(request.getParameter("id"))) {
                return ResultObject.DELETE_SUCCESS;
            } else{
                return ResultObject.DELETE_SUCCESS;
            }
        } else {
            return new ResultObject(404, "缺少参数");
        }
    }

    @RequestMapping(value = "roleListAll",method = RequestMethod.GET)
    public DataGridView getListAll(){
        try {
            RoleVo roleVo = new RoleVo();
            roleVo.setCompanyId(CompanyId);
            return roleService.getRoleListAll(roleVo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new DataGridView();
        }

    }
}

